package com.edais.service.imp;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edais.dao.ErrorMessageSettingDao;
import com.edais.domain.ErrorMessageSetting;
import com.edais.service.ErrorMessageService;
import com.edais.util.Message;
@Service

@Transactional
public class ErrorMessageServiceImpl implements ErrorMessageService {

	@Autowired
	ErrorMessageSettingDao messageSettingDao ;

	@Override
	public ResultDTO getResultDtoByErrorMessge(Map<String, Object> paramMap) {
		if (paramMap==null)
			return null;
		ErrorMessageSetting errorMessageSetting=(ErrorMessageSetting)paramMap.get("errorMessage");
		if(errorMessageSetting==null)
			return null;
        return new ResultDTO(ResultCode.FAILED.getCode(),errorMessageSetting.getMessage_content(),errorMessageSetting.getMessage_level());
	}

	@Override
	public Map<String, Object> getErrorMessage(String messageName) throws IllegalArgumentException, IllegalAccessException {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> requestMap = new HashMap<String,Object>();
		requestMap.put("messageName", messageName);
		ErrorMessageSetting ems = messageSettingDao.selectErrorMessageSettingByName(requestMap);
		String messageContent = "";
		if(ems!=null){
			messageContent = ems.getMessage_content();
		}else{ 
			ems = new ErrorMessageSetting();
			ems.setMessage_name(messageName);
			//利用反射获取
			Message.CnDescriptionExpression stn = new Message().new CnDescriptionExpression(); 
			Field fields[]= stn.getClass().getDeclaredFields();
			
			for (int i = 0; i < fields.length; i++) {
			   Field field= field=fields[i];
			   field.setAccessible(true);//修改访问权限
			   if (messageName.equals(field.getName())) {
			    	messageContent = field.get(stn).toString();
			    	break;
			   }
			}
			ems.setMessage_content(messageContent);
			ems.setMessage_level("1");
			ems.setMessage_desc("");
		}
		
		
		if(StringUtils.isBlank(messageContent)){
			ems = new ErrorMessageSetting();
			ems.setMessage_name("BUSINESSE_ERROR");
			ems.setMessage_desc("网络连接异常，请稍后再试 。");
			ems.setMessage_content(Message.CnDescriptionExpression.BUSINESSE_ERROR);
			ems.setMessage_level("1");
		}
		resultMap.put("errorMessage", ems);
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getErrorMessage(String messageName,
			String[] arrgs) throws IllegalArgumentException,
			IllegalAccessException {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> requestMap = new HashMap<String,Object>();
		requestMap.put("messageName", messageName);
		ErrorMessageSetting ems = messageSettingDao.selectErrorMessageSettingByName(requestMap);
		String messageContent = "";
		if(ems!=null){
			messageContent = ems.getMessage_content();
		}else{ 
			ems = new ErrorMessageSetting();
			ems.setMessage_name(messageName);
			//利用反射获取
			Message.CnDescriptionExpression stn = new Message().new CnDescriptionExpression(); 
			Field fields[]= stn.getClass().getDeclaredFields();
			
			for (int i = 0; i < fields.length; i++) {
			   Field field= field=fields[i];
			   field.setAccessible(true);//修改访问权限
			   if (messageName.equals(field.getName())) {
			    	messageContent = field.get(stn).toString();
			    	break;
			   }
			}
			ems.setMessage_content(messageContent);
			ems.setMessage_level("1");
			ems.setMessage_desc("");
		}
		
		
		if(StringUtils.isBlank(messageContent)){
			ems = new ErrorMessageSetting();
			ems.setMessage_name("BUSINESSE_ERROR");
			ems.setMessage_desc("网络连接异常，请稍后再试 。");
			ems.setMessage_content(Message.CnDescriptionExpression.BUSINESSE_ERROR);
			ems.setMessage_level("1");
		}else{
			for(int i = 0;i<arrgs.length;i++){
				messageContent = messageContent.replace("{"+i+"}", arrgs[i]);
			}
			ems.setMessage_content(messageContent);
		}
		resultMap.put("errorMessage", ems);
		return resultMap;
	}

}
